/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.user.impl.dao.OrganizationUserDao;
import com.jiantong.user.impl.manager.UserManager;
import com.jiantong.user.model.OrganizationUser;
import com.jiantong.user.model.User;
import com.jiantong.user.service.OrganizationUserWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : panxin
 */
@Slf4j
@Service
@RpcProvider
public class OrganizationUserWriteServiceImpl implements OrganizationUserWriteService {

    private final UserManager userManager;

   private OrganizationUserDao organizationUserDao;
    @Autowired
    public OrganizationUserWriteServiceImpl(UserManager userManager,OrganizationUserDao organizationUserDao) {
        this.userManager = userManager;
        this.organizationUserDao=organizationUserDao;
    }

    @Override
    public Response<Boolean> createGovernmentUser(User user, OrganizationUser organizationUser) {
        try {
            // TODO check and set user or org-user roles
            checkUser(user);
            return Response.ok(userManager.createOrganizationUser(user, organizationUser));
        }catch (Exception e) {
            log.error("failed to create government user, user = {}, organization user = {}, cause :{}",
                    user, organizationUser, Throwables.getStackTraceAsString(e));
            return Response.fail("create.user.failed");
        }
    }

    @Override
    public Response<Boolean> createButcherFactoryUser(User user, OrganizationUser organizationUser) {
        try {
            // TODO check and set user or org-user roles
            checkUser(user);
            return Response.ok(userManager.createOrganizationUser(user, organizationUser));
        }catch (Exception e) {
            log.error("failed to create butcher factory user, user = {}, organization user = {}, cause :{}",
                    user, organizationUser, Throwables.getStackTraceAsString(e));
            return Response.fail("create.user.failed");
        }
    }

    @Override
    public Response<Boolean> createBreederFactoryUser(User user, OrganizationUser organizationUser) {
        try {
            // TODO check and set user or org-user roles
            checkUser(user);
            return Response.ok(userManager.createOrganizationUser(user, organizationUser));
        }catch (Exception e) {
            log.error("failed to create breeder factory user, user = {}, organization user = {}, cause :{}",
                    user, organizationUser, Throwables.getStackTraceAsString(e));
            return Response.fail("create.user.failed");
        }
    }


    public Response<Long> create1( OrganizationUser organizationUser) {
        try{
            organizationUserDao.create(organizationUser);
            return Response.ok(organizationUser.getId());
        }catch (Exception e){
            log.error("failed to create breeder factory user, user = {}, organization user = {}, cause :{}",
                     organizationUser, Throwables.getStackTraceAsString(e));
            return Response.fail("create.user.failed");
        }
    }

    private void checkUser(User user) {
//        if (user.getRoles() == null || user.getRoles().isEmpty()) {
//            throw new ServiceException("user.roles.is.empty");
//        }
    }
}
